<template>
  <!-- <div> -->
    <!-- 测试看数据用的
      <h1>
      总数：{{ total }}、页大小：{{ pageSize }}、页码：{{
        pageNo
      }}、连续页数：{{ continues }}
    </h1>
    <h2>总页数：{{ totalPage }}</h2>
    <h1>
      直接收到的 => 总数:{{ total }}、页码:{{ pageNo }}、页大小:{{
        pageSize
      }}、连续{{ continues }}
    </h1>
    <h1>
      算出来的 => 总页数:{{ totalPage }}、连续起始页码:{{
        startEnd.start
      }}、连续结束页码:{{ startEnd.end }}
    </h1>
  </div> -->

  <div class="pagination">    
    	<button @click="getPageNo(pageNo - 1)" :disabled=" pageNo === 1">上一页</button>    
        <button @click="getPageNo(1)" v-show="startEnd.start > 1">1</button>    
        <span v-show="startEnd.start > 2">···</span>  
        <!-- 不用的数据可以用下划线代替 -->
        <!-- 遍历生成连续页 -->
        <button
        v-for="(_,index) in startEnd.end - startEnd.start + 1" :key="index"
        :class="{active:(index + startEnd.start) === pageNo}"
        @click="getPageNo(index + startEnd.start)"
        >
        {{index + startEnd.start }}
        </button>    
          
        
        <span  v-show="startEnd.end < totalPage - 1">···</span>    
        <button @click="getPageNo(totalPage)" v-show="startEnd.end < totalPage ">{{totalPage}}</button>    
        <button @click="getPageNo(pageNo + 1)" :disabled=" pageNo === totalPage">下一页</button>   
        <span>共 {{total}} 条</span>  
    </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["total", "pageNo", "pageSize", "continues","getPageNo"],
  computed: {
    //总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    //计算连续分页的起始、结束位置
    startEnd() {
      //解构赋值拿到分页器的所有相关参数
      const { total, pageNo, pageSize, continues, totalPage } = this;
      //提前准备好起始、结束
      let start = 0; //起始页
      let end = 0; //结束页

      /* 计算开始 */
      //一上来，马上就得判断：continues > totalPage
      //举例理解：我一共有4个苹果，而你却要连续的5个，臣妾做不到，只能给你1到4
      if (continues > totalPage) {
        start = 1; //起始页码为1
        end = totalPage; //结束页码为最后一页
      } else {
        //根据pageNo，往左推，算出start
        start = pageNo - parseInt(continues / 2);
        //根据pageNo，往右推，算出end
        end = pageNo + parseInt(continues / 2);
        //若左边推“冒”了，左边直接置为1（左重置	）
        if (start < 1) {
          start = 1;
          end = continues;
        }
        //若右边推“冒”了，起始直接为：totalPage-continues+1 ，结束直接为totalPage（右重置	）
        if (end > totalPage) {
          start = totalPage - continues + 1;
          end = totalPage;
        }
      }
      /* 计算结束 */
      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: gray;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;
    &[disabled] {
      color: #c0c4cc;
      cursor: pointer;
    }
    &.active {
      cursor: pointer;
      background-color: #c81623;
      color: #fff;
    }
  }
  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>
